//
//  LZAuthView.m
//  laziz_Merchant
//
//  Created by ZhaoDongBo on 2016/11/23.
//  Copyright © 2016年 XBN. All rights reserved.
//

#import "LZAuthView.h"
#import "LZUserInfoView.h"
#import "KDCycleBannerView.h"

#import "GVUserDefaults+LZProperties.h"
#import "UIImageView+WebCache.h"
#import "LZUserModel.h"
#import "Masonry.h"
#import "HCSStarRatingView.h"

@interface LZAuthView()<KDCycleBannerViewDataSource, KDCycleBannerViewDelegate>

@end

@implementation LZAuthView

- (instancetype)initWithFrame:(CGRect)frame {
    
    self = [super initWithFrame:frame];
    if (self) {
        
        [self configView];
        [self addAutoLayout];
    }
    return self;
}

- (void)configView {

    [self addSubview:self.titleLabel];
    [self addSubview:self.subLabel];
    [self addSubview:self.authButton];
    
    [self addSubview:self.cycleBannerView];
    [self addSubview:self.userInfoView];
}

- (void)addAutoLayout {

    WS(weakSelf);
    
    [self.titleLabel makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(68.5);
        make.centerX.mas_equalTo(weakSelf.centerX);
        make.height.mas_equalTo(40);
    }];
    
    [self.subLabel makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(weakSelf.titleLabel.mas_bottom).with.offset(15);
        make.leading.mas_equalTo(40);
        make.trailing.mas_equalTo(-40);
        make.height.mas_equalTo(40);
    }];
    
    [self.authButton makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(weakSelf.subLabel.mas_bottom).with.offset(20);
        make.centerX.mas_equalTo(weakSelf.centerX);
        make.size.mas_equalTo(CGSizeMake(189, 30));
    }];
    
    [self.cycleBannerView makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(0);
        make.leading.mas_equalTo(0);
        make.trailing.mas_equalTo(0);
        make.height.mas_equalTo(235);
    }];
    
    [self.userInfoView makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(235);
        make.leading.mas_equalTo(0);
        make.bottom.mas_equalTo(0);
        make.trailing.mas_equalTo(0);
    }];
}

- (void)layoutSubviews {
    [super layoutSubviews];
    
    if ([GVUserDefaults standardUserDefaults].authType == kAuthType_completeAuth) {
        self.cycleBannerView.hidden = NO;
        self.userInfoView.hidden = NO;
        self.authButton.hidden = YES;
    }else {
        self.cycleBannerView.hidden = YES;
        self.userInfoView.hidden = YES;
        self.authButton.hidden = NO;
    }
}

- (void)reloadFocusView
{
    WS(weakSelf)
    [_cycleBannerView reloadDataWithCompleteBlock:^{
        if (weakSelf.focusArrays.count > 1) {
            weakSelf.cycleBannerView.autoPlayTimeInterval = 5;
        }
        else {
            weakSelf.cycleBannerView.autoPlayTimeInterval = 0;
        }
    }];
}

- (void)authButtonAction:(id)sender {

    if (self.authButtonActionBlock) {
        self.authButtonActionBlock(sender);
    }
}

#pragma mark - KDCycleBannerViewDataSource

- (NSArray *)numberOfKDCycleBannerView:(KDCycleBannerView *)bannerView {
    return self.focusArrays;
}

- (UIViewContentMode)contentModeForImageIndex:(NSUInteger)index {
    return UIViewContentModeScaleToFill;
}

- (UIImage *)placeHolderImageOfBannerView:(KDCycleBannerView *)bannerView atIndex:(NSUInteger)index {
    return [UIImage imageNamed:@"lz_cycle_placehoiderImage"];
}

- (UIImage *)placeHolderImageOfZeroBannerView {
    return [UIImage imageNamed:@"lz_cycle_placehoiderImage"];
}

#pragma mark - KDCycleBannerViewDelegate

- (void)cycleBannerView:(KDCycleBannerView *)bannerView didScrollToIndex:(NSUInteger)index {
    DLog(@"didScrollToIndex:%ld", (long)index);
}

- (void)cycleBannerView:(KDCycleBannerView *)bannerView didSelectedAtIndex:(NSUInteger)index {
    DLog(@"didSelectedAtIndex:%ld", (long)index);
}

#pragma mark - Setter/Getter

- (UILabel *)titleLabel {

    if (_titleLabel == nil) {
        _titleLabel = [[UILabel alloc] init];
        _titleLabel.text            = @"laziz!";
        _titleLabel.font            = [UIFont systemFontOfSize:36];
        _titleLabel.textColor       = UIColorFromRGB(0xffffff, 1);
        _titleLabel.textAlignment   = NSTextAlignmentCenter;
    }
    
    return _titleLabel;
}

- (UILabel *)subLabel {
    
    if (_subLabel == nil) {
        _subLabel = [[UILabel alloc] init];
        _subLabel.text          = LZLocalizedString(@"auth_title_str", nil);
        _subLabel.font          = [UIFont fontWithName:@"Menlo-Italic" size:17];
        _subLabel.textColor     = UIColorFromRGB(0x999999, 1);
        _subLabel.numberOfLines = 2;
        _subLabel.textAlignment = NSTextAlignmentCenter;
    }

    return _subLabel;
}

- (UIButton *)authButton {

    if (_authButton == nil) {
        _authButton = [[UIButton alloc] init];
        _authButton.titleLabel.font     = [UIFont boldSystemFontOfSize:13];
        _authButton.backgroundColor     = UIColorFromRGB(0x33a928, 1);
        _authButton.layer.masksToBounds = YES;
        _authButton.layer.cornerRadius  = 15;
        [_authButton setTitle:LZLocalizedString(@"auth_button_str", nil) forState:UIControlStateNormal];
        [_authButton setTitleColor:UIColorFromRGB(0xffffff, 1) forState:UIControlStateNormal];
        [_authButton addTarget:self action:@selector(authButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    }
    
    return _authButton;
}

- (KDCycleBannerView *)cycleBannerView {

    if (_cycleBannerView == nil) {
        _cycleBannerView = [[KDCycleBannerView alloc] init];
        _cycleBannerView.datasource = self;
        _cycleBannerView.delegate = self;
        _cycleBannerView.continuous = YES;
        _cycleBannerView.hidden = YES;
    }
    
    return _cycleBannerView;
}

- (LZUserInfoView *)userInfoView {

    if (_userInfoView == nil) {
        _userInfoView = [[LZUserInfoView alloc] init];
        _userInfoView.hidden = YES;
    }
    
    return _userInfoView;
}

- (void)setModel:(LZMerchantInfoModel *)model {
    
    _model = model;
    
    [self.userInfoView.headImageView sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@", APIBaseImageUrl, model.sellerPhoto]]
                                                  placeholderImage:[UIImage imageNamed:@"lz_user_placehoiderImage"]];
    
    if ([model.remarkCountPerson integerValue] > 0) {
        self.userInfoView.infoLabel.text = [NSString stringWithFormat:@"%@: %0.1f   %@: %0.1f   %@: %0.1f", LZLocalizedString(@"taste", nil), [model.remarkCountTaste floatValue]/[model.remarkCountPerson floatValue], LZLocalizedString(@"atmosphere", nil),[model.remarkCountEnv floatValue]/[model.remarkCountPerson floatValue], LZLocalizedString(@"service", nil), [model.remarkCountService floatValue]/[model.remarkCountPerson floatValue]];
    }
    else {
        self.userInfoView.infoLabel.text = [NSString stringWithFormat:@"%@: 0.0   %@: 0.0   %@: 0.0", LZLocalizedString(@"taste", nil), LZLocalizedString(@"atmosphere", nil), LZLocalizedString(@"service", nil)];
    }

    [self.userInfoView setCollectNum:[NSString stringWithFormat:@"%ld", (long)[model.storeCount integerValue]] andCommentNum:[NSString stringWithFormat:@"%ld", (long)[model.remarkCountPerson integerValue]]];
    
    self.userInfoView.starRatingView.value = model.averageScore;
}

- (void)setFocusArrays:(NSMutableArray *)focusArrays {

    if (focusArrays.count > 0) {
        _focusArrays = focusArrays;
        [self reloadFocusView];
    }
}

@end
